package com.code.wflow.service.impl;

import com.code.wflow.constants.TaskFinishEnum;
import com.code.wflow.mapper.*;
import com.code.wflow.model.FormItem;
import com.code.wflow.model.Workflow;
import com.code.wflow.model.WorkflowNode;
import com.code.wflow.service.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程定义表(Workflow)表服务层
 *
 * @author ggh
 * @date 2022/10/26
 */
@Service
public class WorkflowServiceImpl implements WorkflowService {
    @Autowired
    private WorkflowMapper workflowMapper;
    @Autowired
    private FormItemMapper formItemMapper;
    @Autowired
    private InstanceMapper instanceMapper;
    @Autowired
    private WorkflowNodeMapper workflowNodeMapper;
    @Autowired
    private WorkflowTaskMapper taskMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Workflow queryById(Long id) {
        return this.workflowMapper.queryById(id);
    }

    /**
     * 查询指定行数据
     *
     * @param keyword 关键字
     * @param status  状态
     * @return 对象列表
     */
    @Override
    public List<Workflow> queryByConditions(String keyword, Byte status) {
        return this.workflowMapper.queryByConditions(keyword, status);
    }


    /**
     * 新增数据
     *
     * @param workflow         流程信息表
     * @param workflowNodeList 流程定义表
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(Workflow workflow, List<WorkflowNode> workflowNodeList, List<FormItem> formItemList) {
        if (this.workflowMapper.insert(workflow) <= 0) {
            return false;
        }
        if (this.workflowNodeMapper.insertList(workflowNodeList, workflow.getId()) != workflowNodeList.size()) {
            throw new RuntimeException();
        }
        if (this.formItemMapper.insertList(formItemList, workflow.getId()) != formItemList.size()) {
            throw new RuntimeException();
        }

        return true;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id, Long taskCount) {
        if (this.workflowMapper.deleteById(id) <= 0) {
            return false;
        }
        if (this.taskMapper.updateTaskFinishByWorkflowId(id, TaskFinishEnum.TRUE.isFinish()) != taskCount) {
            throw new RuntimeException();
        }

        return true;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id, List<Long> instanceIdList) {
        if (this.workflowMapper.deleteById(id) <= 0) {
            return false;
        }
        if (this.instanceMapper.deleteByIdList(instanceIdList) != instanceIdList.size()) {
            throw new RuntimeException();
        }
        return true;
    }

    /**
     * 通过主键删除数据
     *
     * @param idList 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIdList(List<Integer> idList) {
        if (this.workflowMapper.deleteByIdList(idList) == idList.size()) {
            return true;
        }
        throw new RuntimeException();
    }
}
